<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Table Component Demo</title>
    <style>
        .container {
            padding: 20px;
        }
        .custom-table-wrapper {
            margin-top: 20px;
        }
        /* Adding custom table style */
        .custom-table {
            width: 100%;
            border-collapse: collapse;
            font-family: Arial, sans-serif;
        }
        
        .custom-table th,
        .custom-table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        
        .custom-table th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        
        .custom-table tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        
        .custom-table tr:hover {
            background-color: #f5f5f5;
        }
        
        .btn {
            display: inline-block;
            padding: 4px 8px;
            margin: 2px;
            text-decoration: none;
            border-radius: 4px;
            cursor: pointer;
            border: none;
            font-size: 12px;
        }
        
        .btn-primary {
            background-color: #007bff;
            color: white;
        }
        
        .btn-danger {
            background-color: #dc3545;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #0056b3;
        }
        
        .btn-danger:hover {
            background-color: #c82333;
        }
        
        /* Style for edit inputs */
        .edit-input {
            width: calc(100% - 16px);
            padding: 4px;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Table Readonly Demo</h1>
        
        <!-- Define table structure using custom column tags -->
        <div id="my-table">
            <column field="id" title="ID" width="100px" sort="true"></column>
            <column field="name" title="Name" width="200px"></column>
            <column field="email" title="Email" width="250px">{{id}}+{{email}}</column>
            <!-- <column field="action" title="Action" width="150px">
                <button class="btn btn-primary" onclick="editUser({{$index}})">Edit</button>
                <button class="btn btn-danger" onclick="deleteUser({{$index}})">Delete</button>
            </column> -->
        </div>
        
        <button class="btn btn-primary" onclick="loadData()">Load Data</button>
        <button class="btn btn-primary" onclick="addNewRow()">Add New Row</button>
    </div>
    <div class="container 2">
        <h1>Table Editable Demo</h1>
        
        <!-- Define table structure using custom column tags -->
        <div id="my-table2" editable="true">
            <column field="id" title="ID" width="100px" sort="true"></column>
            <column field="name" title="Name" width="200px"></column>
            <column field="email" title="Email" width="250px">{{id}}+{{email}}</column>
            <column field="action" title="Action" width="150px">
                <button class="btn btn-primary" onclick="editUser({{$index}})">Edit</button>
                <button class="btn btn-danger" onclick="deleteUser({{$index}})">Delete</button>
            </column>
        </div>
        
        <button class="btn btn-primary" onclick="loadData()">Load Data</button>
        <button class="btn btn-primary" onclick="addNewRow()">Add New Row</button>
    </div>
    <!-- Include the table component script -->
    <script src="table_component.js"></script>
    
    <script>
        // Initialize the table component
        const tableComponent = new TableComponent('#my-table');
        window.tableComponent = tableComponent; // Make it globally accessible
        
        const editableTableComponent = new TableComponent('#my-table2');

        // Sample data for demonstration
        const sampleData = [
            { id: 1, name: 'John Doe', email: 'john@example.com' },
            { id: 2, name: 'Jane Smith', email: 'jane@example.com' },
            { id: 3, name: 'Bob Johnson', email: 'bob@example.com' }
        ];
        
        // Function to load data into the table
        function loadData() {
            // In a real application, this would fetch data from a server
            tableComponent.data = sampleData;
            tableComponent.renderData();

            editableTableComponent.data = sampleData;
            editableTableComponent.renderData();
        }
        
        // Function to add a new row
        function addNewRow() {
            editableTableComponent.addNewRow();
        }
        
        // Example action functions
        function editUser(id) {
            // This will be overridden by the global function in TableComponent
            // alert('Edit user with ID: ' + id);
            editableTableComponent.editRow(id);
        }
        
        function deleteUser(id) {
            alert('Delete user with ID: ' + id);
        }

        window.onload = function () {
            loadData();
        }
    </script>
</body>
</html>

